package com.example.shoppingmall.cotroller;

import com.alibaba.fastjson.JSONArray;
import com.example.shoppingmall.pojo.*;
import com.example.shoppingmall.service.*;
import com.example.shoppingmall.service.sms.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class AdminController {
    @Autowired
    private SMSCode_register smsCode_register;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AdminJurisdictionService adminJurisdictionService;
    @Autowired
    private NoteService noteService;

    public void setSmsCode_register(SMSCode_register smsCode_register) {
        this.smsCode_register = smsCode_register;
    }

    public void setNoteService(NoteService noteService) {
        this.noteService = noteService;
    }

    public void setAdminJurisdictionService(AdminJurisdictionService adminJurisdictionService) {
        this.adminJurisdictionService = adminJurisdictionService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    /**
     * 跳转到管理员界面
     *
     * @param no
     * @param session
     * @return
     */
    @RequestMapping("/toAdminList")
    public String toAdminList(@RequestParam(value = "no", required = false) String no, HttpSession session) {
        int curr = 1;
        Page page = new Page();
        int count = adminService.count();
        page.setCount(count);
        if (no != null) {
            curr = Integer.parseInt(no);
            if (curr > page.getPageSum()) {
                curr = page.getPageSum();
            } else if (curr < 1) {
                curr = 1;
            }
        }
        page.setPageNo(curr);
        List<Admin> adminList = adminService.adminList((curr - 1) * 6);
        List<Role> roleList = roleService.roleList();
        session.setAttribute("page", page);
        session.setAttribute("adminList", adminList);
        session.setAttribute("roleList", roleList);
        return "user/adminList";
    }

    /**
     * 管理员新增
     *
     * @param account
     * @param phone
     * @param password
     * @param adminName
     * @param roleId
     * @param session
     * @return
     */
    @RequestMapping(value = "/toAdminAdd")
    public String toAdminAdd(@RequestParam("account") String account, @RequestParam("phone") String phone,
                             @RequestParam("password") String password, @RequestParam("adminName") String adminName,
                             @RequestParam("roleId") String roleId, HttpSession session) throws Exception {

        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder(10);
        String encoderpwd=encoder.encode(password);
        Admin admin = new Admin();
        admin.setAccount(account);
        admin.setPhone(phone);
        admin.setPassword(encoderpwd);
        admin.setAdminName(adminName);
        admin.setRoleId(Integer.parseInt(roleId));
        int rs = adminService.add(admin);
        if (rs > 0) {
            //发短信
            //smsCode_register.sendCode(phone);
            //存短信
            Note note=new Note();
            note.setPhone(phone);
            note.setTypeId(1);
            note.setContentId(1);
            noteService.add(note);
            return "redirect:toAdminList";
        } else {
            return "edirect:toUserList";
        }
    }

    /**
     * 判断账号是否存在
     *
     * @param account
     * @return
     */
    @RequestMapping(value = "/chackAccount")
    @ResponseBody
    public Object chackAccount(@RequestParam("account") String account) {
        Admin admin = adminService.chackAccount(account);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (admin != null) {
            map.put("checking", "exists");
        } else {
            map.put("checking", "no");
        }
        return JSONArray.toJSONString(map);
    }

    /**
     * 判断手机号是否存在
     *
     * @param phone
     * @return
     */
    @RequestMapping(value = "/chackAdminPhone")
    @ResponseBody
    public Object chackPhone(@RequestParam("phone") String phone) {
        Admin admin = adminService.chackPhone(phone);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (admin != null) {
            map.put("checking", "exists");
        } else {
            map.put("checking", "no");
        }
        return JSONArray.toJSONString(map);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/toAdminDelete")
    @ResponseBody
    public Map<Object,Object> toAdminDelete(@RequestParam("id") String id) {
        Map<Object,Object> map=new HashMap<>();
        int rs = adminService.delete(Integer.parseInt(id));
        if (rs > 0) {
            map.put("error","yes");
        } else {
            map.put("error","no");
        }
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @param session
     * @return
     */
    @RequestMapping(value = "/toAdminXiangQing")
    public String toAdminXiangQing(@RequestParam("id") String id, HttpSession session) {
        Admin admin = adminService.xiangQing(Integer.parseInt(id));
        session.setAttribute("admin", admin);
        return "user/adminUpdate";
    }

    /**
     * admin信息修改
     *
     * @param id
     * @param phone
     * @param password
     * @return
     */
    @RequestMapping(value = "/toAdminUpdate")
    public String toAdminUpdate(@RequestParam("id") String id,
                                @RequestParam("phone") String phone, @RequestParam("password") String password) {
        Admin admin = new Admin();
        if (password == "" || password == null) {
            admin.setId(Integer.parseInt(id));
            admin.setPhone(phone);
            int rs = adminService.update1(admin);
            if (rs > 0) {
                return "redirect:toAdminList";
            } else {
                return "redirect:toAdminList";
            }
        }else{
            admin.setId(Integer.parseInt(id));
            admin.setPassword(password);
            admin.setPhone(phone);
            int rs = adminService.update(admin);
            if (rs > 0) {
                return "redirect:toAdminList";
            } else {
                return "redirect:toAdminList";
            }
        }
    }

    /**
     * 查看管理员角色
     * @param adminId
     * @param session
     * @return
     */
    @RequestMapping(value = "/toadminJurisdiction")
    public String adminJurisdiction(@RequestParam("adminId")String adminId,HttpSession session){
        Admin admin=adminService.xiangQing(Integer.parseInt(adminId));
        List<AdminJurisdiction>adminJurisdictionList=adminJurisdictionService.AdminJurisdictionList(Integer.parseInt(adminId));
        session.setAttribute("adminJurisdictionList",adminJurisdictionList);
        session.setAttribute("admin",admin);
        return "user/adminJurisdiction";
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping(value = "/toAdminJurisdictionDelete")
    public String toAdminJurisdictionDelete(@RequestParam("id")String id){
        int rs=adminJurisdictionService.delete(Integer.parseInt(id));
        if (rs > 0) {
            return "redirect:toAdminList";
        } else {
            return "redirect:toAdminList";
        }
    }

    /**
     * 跳转到增加角色信息页面
     * @param session
     * @return
     */
    @RequestMapping(value = "/toAdminJurisdictionAdd")
    public String toAdminJurisdictionAdd(HttpSession session,@RequestParam("adminId")String adminId){
        List<Role> roleList = roleService.roleList();
        session.setAttribute("roleList",roleList);
        session.setAttribute("adminId",adminId);
        return "user/adminJurisdictionAdd";
    }

    /**
     * 增加操作
     * @param adminId
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/AdminJurisdictionAdd")
    public String AdminJurisdictionAdd(@RequestParam("adminId")String adminId,@RequestParam("roleId")String roleId,
          Model model){
           AdminJurisdiction adminJurisdiction1=adminJurisdictionService.chackRoleId(Integer.parseInt(roleId),Integer.parseInt(adminId));
           if(adminJurisdiction1!=null){
               model.addAttribute("error","该角色此管理员已拥有!");
               return "user/adminJurisdictionAdd";
           }else {
               AdminJurisdiction adminJurisdiction = new AdminJurisdiction();
               adminJurisdiction.setRoleId(Integer.parseInt(roleId));
               adminJurisdiction.setAdminId(Integer.parseInt(adminId));
               int rs = adminJurisdictionService.add(adminJurisdiction);
               if (rs > 0) {
                   return "redirect:toAdminList";
               } else {
                   return "redirect:toAdminList";
               }
           }
    }
}
